package com.pro.service.imple;

import com.pro.dao.ClosureMapper;
import com.pro.dao.UserListMapper;
import com.pro.entity.*;
import com.pro.service.UserListService;
import com.pro.utils.DateUtils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author ：刘弘禹
 * @description：TODO
 * @date ：2021/11/15 13:45
 */
@Service
public class UserListServiceImple implements UserListService {
    @Autowired
    private UserListMapper userListMapper;

    @Autowired
    private ClosureMapper closureMapper;
    @Override
    public List<BeforeUser> queryBeforeUser(BeforeUser beforeUser) {
        if("".equals(beforeUser.getCreateDate())){
            beforeUser.setCreateDate(null);
        }
        List<BeforeUser> beforeUsers = userListMapper.queryBeforeUser(beforeUser);
        return beforeUsers;
    }

    @Override
    public Boolean updateStudyDate(Integer study, Integer id) {
        Boolean aBoolean = userListMapper.updateStudyDate(study, id);
        return aBoolean;
    }

    @Override
    @Transactional
    public Boolean addClosure(Closure closure) {
        closure.setStartDate(DateUtils.transDate(new Date()));
        Boolean aBoolean = closureMapper.addClosure(closure);
        userListMapper.updateStudyDate(5,closure.getUserId());
        return aBoolean;
    }

    @Override
    public Boolean updateBeforeUser(BeforeUser beforeUser) {
        Boolean aBoolean = userListMapper.updateBeforeUser(beforeUser);
        return aBoolean;
    }

    @Override
    @Transactional
    public Boolean addBeforeUser(BeforeUserPlus beforeUserPlus) {
        beforeUserPlus.setConsumption("0");
        beforeUserPlus.setCreateDate(DateUtils.transDate(new Date()));
        beforeUserPlus.setStudyDate(0);
        beforeUserPlus.setStatus(2);
        UserDevelopment userDevelopment = new UserDevelopment();
        userDevelopment.setProfession(beforeUserPlus.getProfession());
        userDevelopment.setType(beforeUserPlus.getType());
        userDevelopment.setStudyContent(beforeUserPlus.getStudyContent());
        userDevelopment.setIntegration(beforeUserPlus.getIntegration());
        userDevelopment.setEntranceDate(beforeUserPlus.getEntranceDate());
        userDevelopment.setGraduationDate(beforeUserPlus.getGraduationDate());
        userDevelopment.setCreateId(beforeUserPlus.getCreateId());
        userDevelopment.setCreateDate(DateUtils.transString(beforeUserPlus.getCreateDate()));
        Boolean beforeUserPlus1 = userListMapper.addBeforeUser(beforeUserPlus);
        userDevelopment.setUserId(beforeUserPlus.getUserId());
        Boolean aBoolean = userListMapper.addUserDevelopment(userDevelopment);
        return aBoolean;
    }

    @Override
    public List<UserCoupon> queryCoupons() {
        List<UserCoupon> coupons = userListMapper.queryCoupons();
        return coupons;
    }

    @Override
    @Transactional
    public Boolean addUserCouponUsed(Integer couponId,Integer[] obj) {
        Integer arr[] = obj;
        Boolean aBoolean = true;
        for (Integer integer : arr) {
            UserCouponUsed userCouponUsed = new UserCouponUsed();
            userCouponUsed.setCouponId(couponId);
            userCouponUsed.setState(1);
            userCouponUsed.setUserId(integer);
            userCouponUsed.setGetDate(DateUtils.transDate(new Date()));
            aBoolean = userListMapper.addUserCouponUsed(userCouponUsed);
            if(!aBoolean){
               break;
            }
        }
        if(aBoolean){
            userListMapper.reduceCoupon(couponId, obj.length);
        }
        return aBoolean;
    }

    @Override
    public List<CourseOrderPlus> queryJinSiBeforeUser(BeforeUserPlus beforeUser) {
        if("".equals(beforeUser.getCreateDate())){
            beforeUser.setCreateDate(null);
        }
        List<CourseOrderPlus> beforeUsers = userListMapper.queryJinSiBeforeUser(beforeUser);
        return beforeUsers;
    }

    @Override
    public List<BeforeUser> queryWrongJinSiBeforeUser(BeforeUserPlus beforeUser) {
        if("".equals(beforeUser.getCreateDate())){
            beforeUser.setCreateDate(null);
        }
        List<BeforeUser> beforeUsers = userListMapper.queryWrongJinSiBeforeUser(beforeUser);
        return beforeUsers;
    }

    @Override
    public List<ClosurePlus> queryUserLockoutList(ClosurePlus closurePlus) {
        if("".equals(closurePlus.getEndDate())){
            closurePlus.setEndDate(null);
        }
        if("".equals(closurePlus.getStartDate())){
            closurePlus.setStartDate(null);
        }
        List<ClosurePlus> closurePluses = userListMapper.queryUserLockoutList(closurePlus);
        return closurePluses;
    }

    @Override
    @Transactional
    public Boolean unseal(Integer id,Integer userId,Integer createdId) {
        ClosurePlus closurePlus = new ClosurePlus();
        closurePlus.setUserId(userId);
        closurePlus.setType(2);
        List<ClosurePlus> closurePluses = userListMapper.queryUserLockoutList(closurePlus);
        Unseal unseal = new Unseal(null,userId,closurePluses.get(0).getAccount(),
                new Date(), closurePluses.get(0).getReason(),
                closurePluses.get(0).getDays(),2,createdId);
        Boolean aBoolean1 = userListMapper.addUnseal(unseal);
        if(aBoolean1){
            Boolean unseals = userListMapper.unseal(id,userId);
            if(unseals){
                Boolean aBoolean = userListMapper.updateStudyDate(2, userId);
                return aBoolean;
            }
        }
        return false;
    }

    @Override
    public List<UnsealPlus> queryUnseal(UnsealPlus unsealPlus) {
        unsealPlus.setType(2);
        if ("".equals(unsealPlus.getEndDate())){
            unsealPlus.setEndDate(null);
        }
        List<UnsealPlus> unsealPluses = userListMapper.queryUnseal(unsealPlus);
        return unsealPluses;
    }

    @Override
    public Boolean delUnseal(Integer id) {
        Boolean aBoolean = userListMapper.delUnseal(id);
        return aBoolean;
    }

    @Override
    public List<ClosurePlus> getUnsealingTime() {
        List<ClosurePlus> unsealingTime = userListMapper.getUnsealingTime();
        return unsealingTime;
    }


}
